package com.zygh.questionnairesurvey.server;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.*;
import com.zygh.questionnairesurvey.mapper.DeptMapper;
import com.zygh.questionnairesurvey.mapper.ProblemDetailsMapper;
import com.zygh.questionnairesurvey.mapper.ProblemMapper;
import com.zygh.questionnairesurvey.mapper.QuestionnaireMapper;
import com.zygh.questionnairesurvey.util.SpileUtil;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

import static cn.hutool.core.date.DateTime.now;

@Service
public class QuestionnaireService extends ServiceImpl<QuestionnaireMapper, Questionnaire> {

    @Resource
    private QuestionnaireMapper questionnaireMapper;
    @Resource
    private ProblemDetailsMapper problemDetailsMapper;
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private ProblemMapper problemMapper;


    /**
     * 新增，插入所有字段
     *
     * @param questionnaire 新增的记录
     * @return 返回影响行数
     */
    public CommonResult insert(Questionnaire questionnaire) {
        questionnaire.setCreateTime(now());
        questionnaire.setUpdateTime(now());
        int insert = questionnaireMapper.insert(questionnaire);
        if (insert >= 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("新增失败！");
    }


    /**
     * 修改，修改所有字段
     *
     * @param questionnaire 修改的记录
     * @return 返回影响行数
     */
    public CommonResult update(Questionnaire questionnaire) {
        questionnaire.setUpdateTime(now());
        //根据问卷调查id获取数据库原有的问题id lowest字段(删除详情表)
        Questionnaire questionnaire1 = questionnaireMapper.selectOne(Wrappers.<Questionnaire>lambdaQuery()
                .eq(Questionnaire::getId, questionnaire.getId()));
        if (questionnaire.getLowest() != null) {
            String compare = SpileUtil.compare(questionnaire.getLowest(), questionnaire1.getLowest());
            String[] split = compare.split(",");
            for (int i = 0; i < split.length; i++) {
                problemDetailsMapper.delete(Wrappers.<ProblemDetails>lambdaQuery()
                        .eq(ProblemDetails::getWarningsignal, questionnaire1.getWarningsignal())
                        .eq(ProblemDetails::getProblemId, split[i]));
            }

        }
        int i = questionnaireMapper.updateById(questionnaire);
        if (i > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败！");
    }


    /**
     * 删除记录
     *
     * @param id 待删除的记录
     * @return 返回影响行数
     */
    public CommonResult delete(Integer id) {
        Questionnaire questionnaire1 = questionnaireMapper.selectOne(Wrappers.<Questionnaire>lambdaQuery().eq(Questionnaire::getId, id));
        int i = questionnaireMapper.deleteById(id);
        if (i > 0) {
            String lowest = questionnaire1.getLowest();
            String[] split = lowest.split(",");
            for (int j = 0; j < split.length; j++) {
                problemDetailsMapper.delete(Wrappers.<ProblemDetails>lambdaQuery()
                        .eq(ProblemDetails::getWarningsignal, questionnaire1.getWarningsignal())
                        .eq(ProblemDetails::getProblemId, split[j]));
            }
            return new CommonResult().success(questionnaire1);
        }
        return new CommonResult().validateFailed("删除失败！");
    }


    public CommonResult getById(String username) {
        Questionnaire questionnaire = questionnaireMapper.selectOne(Wrappers.<Questionnaire>lambdaQuery()
                .eq(Questionnaire::getWarningsignal, username));
        return new CommonResult().success(questionnaire);

    }

    /**
     * 问题统计
     *
     * @param id 部门id
     * @return
     */
    public CommonResult findProblemInfoById(Integer id) {
        if (id == null) {
            id = 0;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        //获取单位信息（不传id查全部一级部门（大队）信息，传id查二级部门(中队)信息）
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, id)
                .orderByAsc(Dept::getSort));
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        if (id == 0) {
            String num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2(null), questionnaireMapper.count1(null)));
            String num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5(null), questionnaireMapper.count1(null)), 2);
            String counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8(null), questionnaireMapper.count7(null)), 2);
            map1.put("deptName", "交警支队");
            map1.put("toInformant", Double.valueOf(questionnaireMapper.count1(null)));
            map1.put("informant", Double.valueOf(questionnaireMapper.count2(null)));
            map1.put("unactivated", questionnaireMapper.count9(null));
            map1.put("proInformant", num3);
            map1.put("noInformant", Double.valueOf(questionnaireMapper.count4(null)));
            map1.put("number", Double.valueOf(questionnaireMapper.count5(null)));
            map1.put("perNumber", Double.valueOf(num6));
            map1.put("termNum", Double.valueOf(questionnaireMapper.count7(null)));
            map1.put("termProblemNum", Double.valueOf(questionnaireMapper.count8(null)));
            map1.put("termProblemPerNum", counts);
            list.add(map1);
        }
        if (id != 0) {
            int[] child = findChild(id);
            String num3 = "0";
            String num6 = "0";
            String counts = "0";
            if (questionnaireMapper.count2(id) + child[2] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2(id) + child[2], questionnaireMapper.count1(id) + child[1]));
            }
            if (questionnaireMapper.count5(id) + child[5] != 0 && questionnaireMapper.count1(id) + child[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5(id) + child[5], questionnaireMapper.count1(id) + child[1]), 2);

            }
            if (questionnaireMapper.count8(id) + child[8] != 0) {
                counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8(id) + child[8], questionnaireMapper.count7(id) + child[7]), 2);

            }
            Dept dept = deptMapper.selectById(id);
            map2.put("deptName", dept.getName());
            map2.put("toInformant", Double.valueOf(questionnaireMapper.count1(id) + child[1]));
            map2.put("informant", Double.valueOf(questionnaireMapper.count2(id) + child[2]));
            map2.put("unactivated", Double.valueOf(questionnaireMapper.count9(id) + child[9]));
            map2.put("proInformant", num3);
            map2.put("noInformant", Double.valueOf(questionnaireMapper.count4(id) + child[4]));
            map2.put("number", Double.valueOf(questionnaireMapper.count5(id) + child[5]));
            map2.put("perNumber", Double.valueOf(num6));
            map2.put("termNum", Double.valueOf(questionnaireMapper.count7(id) + child[7]));
            map2.put("termProblemNum", Double.valueOf(questionnaireMapper.count8(id) + child[8]));
            map2.put("termProblemPerNum", Double.valueOf(counts));
            list.add(map2);
        }


        for (Dept dept : depts) {
            //查询子级
            int[] child = findChild(dept.getDeptId());
            String num3 = "0";
            String num6 = "0";
            String counts = "0";

            if (questionnaireMapper.count2(dept.getDeptId()) + child[2] != 0 && questionnaireMapper.count1(dept.getDeptId()) + child[1] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2(dept.getDeptId()) + child[2], questionnaireMapper.count1(dept.getDeptId()) + child[1]));
            }
            if (questionnaireMapper.count5(dept.getDeptId()) + child[5] != 0 && questionnaireMapper.count1(dept.getDeptId()) + child[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5(dept.getDeptId()) + child[5], questionnaireMapper.count1(dept.getDeptId()) + child[1]), 2);
            }
            if (questionnaireMapper.count8(dept.getDeptId()) + child[8] != 0 && questionnaireMapper.count7(dept.getDeptId()) + child[7] != 0) {
                counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8(dept.getDeptId()) + child[8], questionnaireMapper.count7(dept.getDeptId()) + child[7]), 2);

            }

            Map<String, Object> map = new HashMap<>();
            map.put("deptName", dept.getName());
            map.put("toInformant", Double.valueOf(questionnaireMapper.count1(dept.getDeptId()) + child[1]));
            map.put("informant", Double.valueOf(questionnaireMapper.count2(dept.getDeptId()) + child[2]));
            map.put("unactivated", Double.valueOf(questionnaireMapper.count9(dept.getDeptId()) + child[9]));
            map.put("proInformant", num3);
            map.put("noInformant", Double.valueOf(questionnaireMapper.count4(dept.getDeptId()) + child[4]));
            map.put("number", Double.valueOf(questionnaireMapper.count5(dept.getDeptId()) + child[5]));
            map.put("perNumber", Double.valueOf(num6));
            map.put("termNum", Double.valueOf(questionnaireMapper.count7(dept.getDeptId()) + child[7]));
            map.put("termProblemNum", Double.valueOf(questionnaireMapper.count8(dept.getDeptId()) + child[8]));
            map.put("termProblemPerNum", Double.valueOf(counts));

            list.add(map);
        }
        return new CommonResult().success(list);
    }

    /**
     * 全部民警+工勤人员问题统计
     *
     * @param id 部门id
     * @return
     */
    public CommonResult findProblemAll(Integer id) {
        if (id == null) {
            id = 0;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        //获取单位信息（不传id查全部一级部门（大队）信息，传id查二级部门(中队)信息）
        List<Dept> depts = deptMapper.selectList(Wrappers.<Dept>lambdaQuery()
                .eq(Dept::getPid, id)
                .orderByAsc(Dept::getSort));
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        if (id == 0) {
            String num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2All(null), questionnaireMapper.count1All(null)));
            String num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5All(null), questionnaireMapper.count1All(null)), 2);
            String counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8All(null), questionnaireMapper.count7All(null)), 2);
            map1.put("deptName", "交警支队");
            map1.put("toInformant", Double.valueOf(questionnaireMapper.count1All(null)));
            map1.put("informant", Double.valueOf(questionnaireMapper.count2All(null)));
            map1.put("unactivated", questionnaireMapper.count9All(null));
            map1.put("proInformant", num3);
            map1.put("noInformant", Double.valueOf(questionnaireMapper.count4All(null)));
            map1.put("number", Double.valueOf(questionnaireMapper.count5All(null)));
            map1.put("perNumber", Double.valueOf(num6));
            map1.put("termNum", Double.valueOf(questionnaireMapper.count7All(null)));
            map1.put("termProblemNum", Double.valueOf(questionnaireMapper.count8All(null)));
            map1.put("termProblemPerNum", counts);
            list.add(map1);
        }
        if (id != 0) {
            int[] child = findChildAll(id);
            String num3 = "0";
            String num6 = "0";
            String counts = "0";
            if (questionnaireMapper.count2All(id) + child[2] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2All(id) + child[2], questionnaireMapper.count1All(id) + child[1]));
            }
            if (questionnaireMapper.count5All(id) + child[5] != 0 && questionnaireMapper.count1All(id) + child[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5All(id) + child[5], questionnaireMapper.count1All(id) + child[1]), 2);

            }
            if (questionnaireMapper.count8All(id) + child[8] != 0) {
                counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8All(id) + child[8], questionnaireMapper.count7All(id) + child[7]), 2);

            }
            Dept dept = deptMapper.selectById(id);
            map2.put("deptName", dept.getName());
            map2.put("toInformant", Double.valueOf(questionnaireMapper.count1All(id) + child[1]));
            map2.put("informant", Double.valueOf(questionnaireMapper.count2All(id) + child[2]));
            map2.put("unactivated", Double.valueOf(questionnaireMapper.count9All(id) + child[9]));
            map2.put("proInformant", num3);
            map2.put("noInformant", Double.valueOf(questionnaireMapper.count4All(id) + child[4]));
            map2.put("number", Double.valueOf(questionnaireMapper.count5All(id) + child[5]));
            map2.put("perNumber", Double.valueOf(num6));
            map2.put("termNum", Double.valueOf(questionnaireMapper.count7All(id) + child[7]));
            map2.put("termProblemNum", Double.valueOf(questionnaireMapper.count8All(id) + child[8]));
            map2.put("termProblemPerNum", Double.valueOf(counts));
            list.add(map2);
        }


        for (Dept dept : depts) {
            //查询子级
            int[] child = findChildAll(dept.getDeptId());
            String num3 = "0";
            String num6 = "0";
            String counts = "0";

            if (questionnaireMapper.count2All(dept.getDeptId()) + child[2] != 0 && questionnaireMapper.count1All(dept.getDeptId()) + child[1] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.count2All(dept.getDeptId()) + child[2], questionnaireMapper.count1All(dept.getDeptId()) + child[1]));
            }
            if (questionnaireMapper.count5All(dept.getDeptId()) + child[5] != 0 && questionnaireMapper.count1All(dept.getDeptId()) + child[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count5All(dept.getDeptId()) + child[5], questionnaireMapper.count1All(dept.getDeptId()) + child[1]), 2);
            }
            if (questionnaireMapper.count8All(dept.getDeptId()) + child[8] != 0 && questionnaireMapper.count7All(dept.getDeptId()) + child[7] != 0) {
                counts = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.count8All(dept.getDeptId()) + child[8], questionnaireMapper.count7All(dept.getDeptId()) + child[7]), 2);

            }

            Map<String, Object> map = new HashMap<>();
            map.put("deptName", dept.getName());
            map.put("toInformant", Double.valueOf(questionnaireMapper.count1All(dept.getDeptId()) + child[1]));
            map.put("informant", Double.valueOf(questionnaireMapper.count2All(dept.getDeptId()) + child[2]));
            map.put("unactivated", Double.valueOf(questionnaireMapper.count9All(dept.getDeptId()) + child[9]));
            map.put("proInformant", num3);
            map.put("noInformant", Double.valueOf(questionnaireMapper.count4All(dept.getDeptId()) + child[4]));
            map.put("number", Double.valueOf(questionnaireMapper.count5All(dept.getDeptId()) + child[5]));
            map.put("perNumber", Double.valueOf(num6));
            map.put("termNum", Double.valueOf(questionnaireMapper.count7All(dept.getDeptId()) + child[7]));
            map.put("termProblemNum", Double.valueOf(questionnaireMapper.count8All(dept.getDeptId()) + child[8]));
            map.put("termProblemPerNum", Double.valueOf(counts));

            list.add(map);
        }
        return new CommonResult().success(list);
    }


    /**
     * 查询工勤人员名单
     *
     * @return
     */
    public CommonResult findWorker(Integer id) {
        if (id == null) {
            id = 0;
        }
        List<Map<String, Object>> list = new ArrayList<>();
        List<Dept> deptByWord = null;
        //查询所有父级部门
        if (id == 0) {
            deptByWord = deptMapper.findDeptByWord(id);
        } else {
            deptByWord = deptMapper.findDeptWordById(id);
        }


        String znum3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.workcount2(null), questionnaireMapper.workcount1(null)));
        String znum6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.workcount5(null), questionnaireMapper.workcount1(null)), 2);
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        if (id == 0) {
            map2.put("deptName", "交警支队");
            map2.put("toInformant", questionnaireMapper.workcount1(null));
            map2.put("informant", questionnaireMapper.workcount2(null));
            map2.put("unactivated", questionnaireMapper.workcount9(null));
            map2.put("proInformant", znum3);
            map2.put("noInformant", questionnaireMapper.workcount4(null));
            map2.put("number", questionnaireMapper.workcount5(null));
            map2.put("perNumber", znum6);
            map2.put("remark", "无");
            list.add(map2);
        }
        if (id != 0) {
            int[] workchild = findWorkChild(id);
            String num3 = "0";
            String num6 = "0";
            if (questionnaireMapper.workcount2(id) + workchild[2] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.workcount2(id) + workchild[2], questionnaireMapper.workcount1(id) + workchild[1]));
            }
            if (questionnaireMapper.workcount5(id) + workchild[5] != 0 && questionnaireMapper.workcount1(id) + workchild[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.workcount5(id) + workchild[5], questionnaireMapper.workcount1(id) + workchild[1]), 2);

            }
            Dept dept = deptMapper.selectById(id);
            map3.put("deptName", dept.getName());
            map3.put("toInformant", Double.valueOf(questionnaireMapper.workcount1(id) + workchild[1]));
            map3.put("informant", Double.valueOf(questionnaireMapper.workcount2(id) + workchild[2]));
            map3.put("unactivated", Double.valueOf(questionnaireMapper.workcount9(id) + workchild[9]));
            map3.put("proInformant", num3);
            map3.put("noInformant", Double.valueOf(questionnaireMapper.workcount4(id) + workchild[4]));
            map3.put("number", Double.valueOf(questionnaireMapper.workcount5(id) + workchild[5]));
            map3.put("perNumber", Double.valueOf(num6));
            list.add(map3);
        }
        deptByWord.forEach(dept -> {
            int[] workChild = findWorkChild(dept.getDeptId());
            String num3 = "0";
            String num6 = "0";
            if (questionnaireMapper.workcount2(dept.getDeptId()) + workChild[2] != 0 && questionnaireMapper.workcount1(dept.getDeptId()) + workChild[1] != 0) {
                num3 = NumberUtil.decimalFormat("#.##%", NumberUtil.div(questionnaireMapper.workcount2(dept.getDeptId()) + workChild[2], questionnaireMapper.workcount1(dept.getDeptId()) + workChild[1]));
            }
            if (questionnaireMapper.workcount5(dept.getDeptId()) + workChild[5] != 0 && questionnaireMapper.workcount1(dept.getDeptId()) + workChild[1] != 0) {
                num6 = NumberUtil.roundStr(NumberUtil.div(questionnaireMapper.workcount5(dept.getDeptId()) + workChild[5], questionnaireMapper.workcount1(dept.getDeptId()) + workChild[1]), 2);
            }

            Map<String, Object> map1 = new HashMap<>();
            map1.put("deptName", dept.getName());
            map1.put("toInformant", Double.valueOf(questionnaireMapper.workcount1(dept.getDeptId()) + workChild[1]));
            map1.put("informant", Double.valueOf(questionnaireMapper.workcount2(dept.getDeptId()) + workChild[2]));
            map1.put("unactivated", Double.valueOf(questionnaireMapper.workcount9(dept.getDeptId()) + workChild[9]));
            map1.put("proInformant", num3);
            map1.put("noInformant", Double.valueOf(questionnaireMapper.workcount4(dept.getDeptId()) + workChild[4]));
            map1.put("number", Double.valueOf(questionnaireMapper.workcount5(dept.getDeptId()) + workChild[5]));
            map1.put("perNumber", num6);
            map1.put("remark", "");
            list.add(map1);
        });

        return new CommonResult().success(list);
    }


    public int[] findChild(Integer id) {
        //查询子级
        List<Dept> dept1 = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getPid, id));
        int[] count = new int[10];
        if (dept1 != null) {
            for (Dept dept2 : dept1) {
                count[1] += questionnaireMapper.count1(dept2.getDeptId());
                count[2] += questionnaireMapper.count2(dept2.getDeptId());
                count[9] += questionnaireMapper.count9(dept2.getDeptId());
                count[4] += questionnaireMapper.count4(dept2.getDeptId());
                count[5] += questionnaireMapper.count5(dept2.getDeptId());
                count[7] += questionnaireMapper.count7(dept2.getDeptId());
                count[8] += questionnaireMapper.count8(dept2.getDeptId());
            }
            return count;
        }
        return null;

    }

    public int[] findChildAll(Integer id) {
        //查询子级
        List<Dept> dept1 = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getPid, id));
        int[] count = new int[10];
        if (dept1 != null) {
            for (Dept dept2 : dept1) {
                count[1] += questionnaireMapper.count1All(dept2.getDeptId());
                count[2] += questionnaireMapper.count2All(dept2.getDeptId());
                count[9] += questionnaireMapper.count9All(dept2.getDeptId());
                count[4] += questionnaireMapper.count4All(dept2.getDeptId());
                count[5] += questionnaireMapper.count5All(dept2.getDeptId());
                count[7] += questionnaireMapper.count7All(dept2.getDeptId());
                count[8] += questionnaireMapper.count8All(dept2.getDeptId());
            }
            return count;
        }
        return null;

    }

    public int[] findWorkChild(Integer id) {
        //查询子级
        List<Dept> dept1 = deptMapper.selectList(Wrappers.<Dept>lambdaQuery().eq(Dept::getPid, id));
        int[] workcount = new int[10];
        if (dept1 != null) {
            for (Dept dept2 : dept1) {
                workcount[1] += questionnaireMapper.workcount1(dept2.getDeptId());
                workcount[2] += questionnaireMapper.workcount2(dept2.getDeptId());
                workcount[9] += questionnaireMapper.workcount9(dept2.getDeptId());
                workcount[4] += questionnaireMapper.workcount4(dept2.getDeptId());
                workcount[5] += questionnaireMapper.workcount5(dept2.getDeptId());
            }
            return workcount;
        }
        return null;

    }

    /**
     * 通过问题id和大队id分页查询具体人员
     *
     * @param pageNum  页码
     * @param pageSize 每页显示条数
     * @param deptId   部门id
     * @param lowest   问题id
     * @return 符合条件的人员信息
     */
    public PageInfo list(int pageNum, int pageSize, Integer deptId, Integer lowest, Integer termStatus) {
        PageInfo<UserInfo> source = PageHelper
                .startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> questionnaireMapper.listPeople(deptId, lowest, termStatus));
        // 需要转换的对象
        PageInfo<UserInfo> target = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(source, target);
        // 对查询的list进行下一步操作，比如类型转换后
        List<UserInfo> collect = source.getList();
        if (!ObjectUtils.isEmpty(collect)) {
            collect.forEach(userInfo -> userInfo
                    .setDeptName(userInfo.getPName() != null
                            ? userInfo.getPName() + "-" + userInfo.getDeptName()
                            : userInfo.getDeptName()));

            //借助commons-collections包的ComparatorUtils
            //BeanComparator，ComparableComparator和ComparatorChain都是实现了Comparator这个接口
            Comparator mycmp = ComparableComparator.getInstance();
            //允许null
            mycmp = ComparatorUtils.nullLowComparator(mycmp);
            //逆序
            mycmp = ComparatorUtils.reversedComparator(mycmp);
            Comparator<UserInfo> cmp;

            cmp = new BeanComparator<>("deptName", mycmp);

            collect.sort(cmp);
        }
        List<UserInfo> hdQueryVos = new ArrayList<>(collect);

        target.setList(hdQueryVos);

        return target;
    }


    public void test() {

        //查询问卷表所有的警号
        List<Questionnaire> questionnaires = questionnaireMapper.selectList(null);

        questionnaires.forEach(questionnaire -> {
            //根据警号查询该民警的所有问题详情
            List<ProblemDetails> problemDetails = problemDetailsMapper.selectList(Wrappers.<ProblemDetails>lambdaQuery()
                    .eq(ProblemDetails::getWarningsignal, questionnaire.getWarningsignal()));
            List<Integer> list = new ArrayList();
            //根据问题详情的问题id 查询该问题的所有父级id
            for (ProblemDetails problemDetail : problemDetails) {
                //查询所有的问题id
//                List<Problem> problemByLastId = problemMapper.findProblemByLastId(problemDetail.getProblemId());
//                for (int i = 0; i < problemByLastId.size(); i++) {
//                    list.add(problemByLastId.get(i).getId());
//                }

            }
            Set set = new HashSet(list);
            list.clear();
            list.addAll(set);
            String string = Arrays.toString(list.toArray()).replaceAll("[^0-9a-zA-Z\u4e00-\u9fa5.，,。？“”]+", "");
            System.out.println("警号：" + questionnaire.getWarningsignal());
            System.out.println("合并后==========：》》》》" + string);
            //插入覆盖
            Questionnaire questionnaire1 = new Questionnaire();
            questionnaire1.setLowest(string);
            questionnaireMapper.update(questionnaire1, Wrappers.<Questionnaire>lambdaQuery()
                    .eq(Questionnaire::getWarningsignal, questionnaire.getWarningsignal()));

        });
    }

}